package com.mall.shopping.services;

import com.alibaba.druid.util.StringUtils;
import com.mall.shopping.IProductCateService;
import com.mall.shopping.converter.ProductCateConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemCat;
import com.mall.shopping.dal.persistence.ItemCatMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dto.AllProductCateRequest;
import com.mall.shopping.dto.AllProductCateResponse;
import com.mall.user.constants.SysRetCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Author : L
 * @ProjectName : pepsi
 * @Date : 1/24/2021
 * @Description : 列举所有商品种类
 */
@Service
@Slf4j
@Component
public class IProductCateServiceImpl implements IProductCateService {

    @Autowired
    ItemCatMapper itemCatMapper;

    @Autowired
    ProductCateConverter productCateConverter;

    @Override
    public AllProductCateResponse getAllProductCate(AllProductCateRequest request) {
        AllProductCateResponse allProductCateResponse = new AllProductCateResponse();

        String sort = request.getSort();
        Example example = new Example(ItemCat.class);
        if (!StringUtils.isEmpty(sort)) {
            if ("1".equals(sort)) {
                example.setOrderByClause(sort + " asc");
            }
            if ("-1".equals(sort)) {
                example.setOrderByClause(sort + " desc");
            }
        }

        List<ItemCat> itemCats = itemCatMapper.selectByExample(example);

        allProductCateResponse.setProductCateDtoList(productCateConverter.items2Dto(itemCats));
        allProductCateResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        allProductCateResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());

        return allProductCateResponse;
    }
}
